import { _decorator, Component, Node, director } from 'cc';
import { Bullet } from './Bullet';
import { EquitmentManager } from './EquitmentManager';
import { GameManager } from './GameManager';
import { JoyStick } from './JoyStick';
const { ccclass, property } = _decorator;

@ccclass('SaveManager')
export class SaveManager extends Component {
    public static instance: SaveManager

    onLoad()
    {
        SaveManager.instance = this
        director.addPersistRootNode(this.node)
    }

    start() {
        
    }

    Save()
    {
        let Data = {
            //version:new Date().getTime(),
            hp:GameManager.instance.PlayerHP, // 生命值
            hpmax:GameManager.instance.PlayerMaxHP, // 最大生命值
            lv:GameManager.instance.Lv, // 等级
            exp:GameManager.instance.ExpCount, // 当前经验值
            movespeed:JoyStick.PLAYER_MOVE_SPEED, // 移速
            bulletdamage:Bullet.BULLET_DAMAGE, // 子弹伤害
            chestloot:GameManager.instance.ChestLootCount, // 宝箱个数
            equits:EquitmentManager.instance.GetAllEquitment(), // 装备拾取顺序，主要用于结算之类的
            curlevel:GameManager.instance.CurLevel, // 当前关卡数
        }
        let json = JSON.stringify(Data)
        console.log("[SaveManager] Save 存档信息：",Data)
        localStorage.setItem("userdata",json)
    }

    Load()
    {
        let data = localStorage.getItem("userdata")
        let json = JSON.parse(data)
        console.log("[SaveManager] Load 读档信息：",json)
        GameManager.instance.PlayerHP = json.hp // 生命值
        GameManager.instance.PlayerMaxHP = json.hpmax // 最大生命值
        GameManager.instance.Lv = json.lv // 等级
        GameManager.instance.ExpCount = json.exp 
        JoyStick.PLAYER_MOVE_SPEED = json.movespeed
        Bullet.BULLET_DAMAGE = json.bulletdamage
        GameManager.instance.ChestLootCount = json.chestloot
        EquitmentManager.instance.SetEquitment(json.equits)
        GameManager.instance.CurLevel = json.curlevel
    }



    onDestroy()
    {
    }
}


